﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNAProject
{
    class BoundingBoxModel
    {
        readonly Color color = Color.Yellow;

        private readonly GraphicsDevice graphicsDevice;

        private static VertexPositionColor[] verts;
        static readonly short[] indices = new short[]
        {
            0, 1,
            1, 2,
            2, 3,
            3, 0,
            0, 4,
            1, 5,
            2, 6,
            3, 7,
            4, 5,
            5, 6,
            6, 7,
            7, 4,
        };

        private readonly Vector3[] corners;

        static BasicEffect effect;
        static VertexDeclaration vertDecl;

        private readonly VertexBuffer vb;
        private readonly IndexBuffer ib;

        public Model Model { get; set; }
        public BoundingBox BoundingBox { get; set; }


        public BoundingBoxModel(Model model, GraphicsDevice graphics)
        {
            Model = model;
            if (model.Tag != null)
            {
                try
                {
                    BoundingBox = (BoundingBox)Model.Tag;
                    graphicsDevice = graphics;

                    effect = new BasicEffect(graphicsDevice, null) { VertexColorEnabled = true, LightingEnabled = false };

                    corners = BoundingBox.GetCorners();

                    verts = new VertexPositionColor[8];
                    for (int i = 0; i < 8; i++)
                    {
                        verts[i].Position = corners[i];
                        verts[i].Color = color;
                    }

                    vertDecl = new VertexDeclaration(graphicsDevice, VertexPositionColor.VertexElements);

                    vb = new VertexBuffer(graphicsDevice, 8 * VertexPositionColor.SizeInBytes, BufferUsage.None);
                    vb.SetData(verts);

                    ib = new IndexBuffer(graphicsDevice, typeof(short), indices.Length, BufferUsage.None);
                    ib.SetData(indices);

                }
                catch (InvalidCastException)
                { }
            }
        }

        public void DrawBoundingBox(Camera camera, Matrix world)
        {
            graphicsDevice.VertexDeclaration = vertDecl;
            graphicsDevice.Vertices[0].SetSource(vb, 0, VertexPositionColor.SizeInBytes);
            graphicsDevice.Indices = ib;

            effect.World = world;
            effect.View = camera.Rotation * camera.View;
            effect.Projection = camera.Projection;

            effect.Begin();
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();

                graphicsDevice.DrawIndexedPrimitives(
                    PrimitiveType.LineList,
                    0,
                    0,
                    8,
                    0,
                    indices.Length / 2);

                pass.End();
            }
            effect.End();
        }
    }
}
